package com.lagou.niancheng.webdemo;

import com.lagou.niancheng.dubbo.api.DubboDemo1Api;
import com.lagou.niancheng.dubbo.api.DubboDemo2Api;
import com.lagou.niancheng.dubbo.filter.TransportIPFilter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class WebDemoController {

    @Reference
    private DubboDemo1Api dubboDemo1Api;

    @Reference
    private DubboDemo2Api dubboDemo2Api;

    @GetMapping("/dubbo/test")
    public String service(HttpServletRequest request) {
        // 将ip地址设置到rpcContext中
        String remoteHost = request.getRemoteHost();
        // 调用远程服务
        TransportIPFilter.setIp(remoteHost);
        String service1Data = dubboDemo1Api.service();
        TransportIPFilter.setIp(remoteHost);
        String service2Data = dubboDemo2Api.service();
        return service1Data + "-" + service2Data;
    }
}
